# -*- coding: utf-8 -*-
# @Author: long
# @Date:   2018-06-02 17:29:40
# @Last Modified by:   ystlong
# @Last Modified time: 2018-06-03 19:35:18

import abc
import os

class PathList(object):
	"""docstring for PathList"""
	def __init__(self):
		super(PathList, self).__init__()

	def list_files(self, conn, handle, cur_abs_dir, cur_relate_dir, depth):
		if depth == 0:
			return
		dirs, files = conn.list(cur_abs_dir)
		for file_name in files:
			try:
				handle(cur_abs_dir, cur_relate_dir, file_name, join_path=conn.join_path)
			except UnicodeDecodeError as e:
				print repr(e)
				pass
		for dir_name in dirs:
			try:
				handle(cur_abs_dir, cur_relate_dir, dir_name, isdir=True, join_path=conn.join_path)
				self.list_files(conn, handle, 
					conn.join_path(cur_abs_dir, dir_name), 
					conn.join_path(cur_relate_dir, dir_name),
					depth-1)
			except UnicodeDecodeError as e:
				print repr(e)
				pass
			
class PathConnect(object):
	__metaclass__  = abc.ABCMeta

	"""docstring for PathConnect"""
	def __init__(self):
		super(PathConnect, self).__init__()

	@abc.abstractmethod
	def list(self, uri):
		pass

	@abc.abstractmethod
	def abs_uri(self, uri):
		pass

	def join_path(self, p1, p2):
		return "%s/%s"%(p1, p2.lstrip("./"))
		# return os.path.join(p1, p2)
	# def get_root_uri(self):
	# 	return self.root_uri

class DefultPathHandle(object):
	"""docstring for DefultPathHandle"""
	def __init__(self):
		super(DefultPathHandle, self).__init__()
	
	def handle(self, cur_dir, file_name, isdir=False, join_path=None):
		if isdir:
			print "d-",
		else:
			print "--",
		print join_path(cur_dir, file_name)


